<%
   metric = widget_properties["metric"] || Metric.by_key('ncloc')

   if measure(metric)

     limit = widget_properties["numberOfLines"]
     title = widget_properties["title"]
     title = message('widget.hotspot_metric.hotspots_by_x', :params => metric.short_name) if title.blank?

     snapshots = nil
     if metric.numeric? && !@snapshot.leaves_qualifiers.empty?
       snapshots_conditions=["snapshots.qualifier in (:qualifiers)", "snapshots.islast=:islast", "snapshots.status = 'P'"]
       snapshots_values={:qualifiers => @snapshot.leaves_qualifiers, :islast => true}
       snapshots_conditions << '(snapshots.root_snapshot_id=:root_sid AND snapshots.path LIKE :path)'
       snapshots_values[:root_sid] = (@snapshot.root_snapshot_id || @snapshot.id)
       snapshots_values[:path]="#{@snapshot.path}#{@snapshot.id}.%"

       measures_conditions = ["project_measures.rule_id IS NULL", "project_measures.characteristic_id IS NULL", "project_measures.person_id IS NULL"]
       measures_values = {}
       measures_conditions << "project_measures.metric_id = :m_id"
       measures_values[:m_id] = metric.id

       measures=ProjectMeasure.find(:all,
                                    :joins => :snapshot,
                                    :conditions => [(snapshots_conditions + measures_conditions).join(' AND '), snapshots_values.merge(measures_values)],
                                    :order => "project_measures.value #{'DESC' if metric.direction<0}",
                                    :limit => limit)

       snapshots=Snapshot.find(measures.map { |m| m.snapshot_id }, :include => 'project')
       snapshots_by_id = {}
       snapshots.each do |s|
         snapshots_by_id[s.id]=s
       end
     end

     unless snapshots && !snapshots.empty? %>
    <h3><%= title -%></h3>
    <span class="empty_widget"><%= message('no_results') -%></span>
  <% else %>

    <div class="line-block">
      <div style="float:right">
        <a class="underlined-link" href="<%= url_for_drilldown(metric) -%>"><%= message('widget.hotspot_metric.more') -%></a>
      </div>
      <h3><%= title -%></h3>
    </div>

    <table id="hotspots-<%= metric.name -%>-<%= widget.id -%>" class="data">
      <thead>
      <tr>
        <th colspan="3"/>
      </tr>
      </thead>
      <tbody>
      <%
         if metric.direction > 0
           metric_max_value = measures.last.value
         else
           metric_max_value = measures.first.value
         end
         measures.each do |measure|
           resource = snapshots_by_id[measure.snapshot_id].resource
      %>
        <tr class="<%= cycle 'even', 'odd', :name => ('hotspot_metric' + widget.id.to_s) -%>">
          <td>
            <%= link_to_resource(resource, h(resource.name), {:metric => metric.name, :class => 'underlined-link'}) -%>
          </td>
          <td class="right nowrap">
            <%= format_measure(measure) -%>
          </td>
          <td class="barchart">
            <div class="barchart" style="width: <%= (measure.value*100/metric_max_value).round.to_i -%>%">
              <div style="width: 100%;"></div>
            </div>
          </td>
        </tr>
      <%
         end
      %>
      </tbody>
    </table>

  <% end %>

<% end %>